package com.tuanbiao.keyevent;

import android.text.TextUtils;
import android.view.KeyEvent;

import com.abiao.lib_utils.StringUtils;
import com.tuanbiao.keyevent.listener.OnBlastKeyDownListener;
import com.tuanbiao.keyevent.listener.OnBlastNettedKeyDownListener;
import com.tuanbiao.keyevent.listener.OnKeyDownListener;
import com.tuanbiao.keyevent.listener.OnLoginKeyDownListener;
import com.tuanbiao.keyevent.listener.OnSettingKeyDownListener;

import java.util.ArrayList;
import java.util.List;

import timber.log.Timber;

/**
 * @author abiao
 * @date 2022/6/6 17:28
 * @description:
 */
public class UrovoKeyEvent implements KeyEventStrategy {
    private StringBuilder keyDownBuf = new StringBuilder();
    private String mInputPsdStr = "";

    @Override
    public String getNettedNoteSuccess() {
        return "提示:按F4进入充电";
    }

    @Override
    public String getNettedNoteFailed() {
        return "，按F1查看错误消息";
    }

    @Override
    public String getChargeNoteSuccess() {
        return "充电完成，可以起爆！请再次确定人员安全撤离！按【F1】+【F4】起爆";
    }

    @Override
    public String getChargeNoteFailed() {
        return "按F2键查看异常雷管信息";
    }

    @Override
    public String getChargeNoteFailed(String msg) {
        return msg + ",按F2键查看充电异常雷管信息";
    }

    @Override
    public String getBlastNoteSuccess() {
        return "爆破完成，可以查看爆破记录！";
    }

    @Override
    public String getBlastNoteFailed() {
        return "起爆异常！请再次确定人员安全撤离！按【F2】查看异常信息";
    }

    @Override
    public String getBlastNoteFailed(String msg) {
        return msg + ",按【F2】查看异常信息";
    }


    @Override
    public List<Integer> getScanModeKeys() {
        List<Integer> keys = new ArrayList<>();
        keys.add(KeyEvent.KEYCODE_F4);
        keys.add(KeyEvent.KEYCODE_F8);
        return keys;
    }

    @Override
    public void checkLoginKeyDown(int keyCode, OnLoginKeyDownListener listener) {
        if (keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_F5) {
            keyDownBuf.append("F1");
            checkLoginKeyDownEvent(listener);
        }
        if (keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_F8) {
            keyDownBuf.append("F4");
            checkLoginKeyDownEvent(listener);
        }
        if (keyCode == KeyEvent.KEYCODE_F2 || keyCode == KeyEvent.KEYCODE_F6) {
            keyDownBuf.append("F2");
            checkLoginKeyDownEvent(listener);
        }
        if (keyCode == KeyEvent.KEYCODE_F3 || keyCode == KeyEvent.KEYCODE_F7) {
            keyDownBuf.append("F3");
            checkLoginKeyDownEvent(listener);
        }
    }

    private void checkLoginKeyDownEvent(OnLoginKeyDownListener listener) {
        if (TextUtils.equals("F1F4", keyDownBuf) || TextUtils.equals("F4F1", keyDownBuf)) {
            listener.onChangeMacIMEI();
        } else if (TextUtils.equals("F2F3", keyDownBuf) || TextUtils.equals("F3F2", keyDownBuf)) {
            listener.onChangeDeviceNum();
        } else {
            postDelayed(() -> {
                if (null != keyDownBuf)
                    keyDownBuf.delete(0, keyDownBuf.length());
            }, FAST_CLICK_INTERVAL);
        }
    }

    @Override
    public void checkBlastKeyDown(int keyCode, OnBlastKeyDownListener listener) {
        if (keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_F5) {
            keyDownBuf.append("F1");
            checkBlastKeyEvent(listener);
        }
        if (keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_F8) {
            keyDownBuf.append("F4");
            checkBlastKeyEvent(listener);
        }
        if (keyCode == KeyEvent.KEYCODE_F2 || keyCode == KeyEvent.KEYCODE_F6) {
            listener.onCheckErrorCaps();
        }
    }

    private void checkBlastKeyEvent(OnBlastKeyDownListener listener) {
        if (TextUtils.equals("F1F4", keyDownBuf) || TextUtils.equals("F4F1", keyDownBuf)) {
            listener.onBlast();
        } else {
            postDelayed(() -> {
                if (null != keyDownBuf)
                    keyDownBuf.delete(0, keyDownBuf.length());
            }, FAST_CLICK_INTERVAL);
        }
    }

    @Override
    public void checkBlastNettedKeyDown(int keyCode, OnBlastNettedKeyDownListener listener) {
        if (keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_F5) {
            listener.onCheckErrorCaps();
        } else if (keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_F8) {
            listener.onCharge();
        }
    }

    @Override
    public void checkSettingKeyDown(int keyCode, OnSettingKeyDownListener listener) {
        if (keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_PERIOD) {
            Timber.i("checkSettingKeyDown:" + mInputPsdStr);
            if (StringUtils.equals("1234", mInputPsdStr)) {
                listener.onMoreVisibility(true);
            } else if (StringUtils.equals("4321", mInputPsdStr)) {
                listener.onMoreVisibility(false);
            }
            mInputPsdStr = "";
        } else if (keyCode == KeyEvent.KEYCODE_1 || keyCode == KeyEvent.KEYCODE_AT) {
            mInputPsdStr += "1";
        } else if (keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_A) {
            mInputPsdStr += "2";
        } else if (keyCode == KeyEvent.KEYCODE_3 || keyCode == KeyEvent.KEYCODE_D) {
            mInputPsdStr += "3";
        } else if (keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_G) {
            mInputPsdStr += "4";
        } else if (keyCode == KeyEvent.KEYCODE_F1) {
            listener.onToggleModel();
        }
        if (keyCode == KeyEvent.KEYCODE_F2 || keyCode == KeyEvent.KEYCODE_F6) {
            keyDownBuf.append("F2");
            checkSettingKeyEvent(listener);
        }
        if (keyCode == KeyEvent.KEYCODE_F3 || keyCode == KeyEvent.KEYCODE_F7) {
            keyDownBuf.append("F3");
            checkSettingKeyEvent(listener);
        }
        if (keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_F5) {
            keyDownBuf.append("F1");
            checkSettingKeyEvent(listener);
        }
        if (keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_F8) {
            keyDownBuf.append("F4");
            checkSettingKeyEvent(listener);
        }
    }

    private void checkSettingKeyEvent(OnSettingKeyDownListener listener) {
        if (TextUtils.equals("F2F3", keyDownBuf) || TextUtils.equals("F3F2", keyDownBuf)) {
            listener.onLogOperate();
        } else if (TextUtils.equals("F1F4", keyDownBuf) || TextUtils.equals("F1F4", keyDownBuf)) {
            listener.onSetting();
        } else {
            postDelayed(() -> {
                if (null != keyDownBuf)
                    keyDownBuf.delete(0, keyDownBuf.length());
            }, 1000);
        }
    }

    @Override
    public void checkUploadDataKeyDown(int keyCode, OnKeyDownListener listener) {
        if (keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_F5) {
            keyDownBuf.append("F1");
            checkF1F4KeyEvent(listener);
        }
        if (keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_F8) {
            keyDownBuf.append("F4");
            checkF1F4KeyEvent(listener);
        }
    }

    private void checkF1F4KeyEvent(OnKeyDownListener listener) {
        if (TextUtils.equals("F1F4", keyDownBuf) || TextUtils.equals("F4F1", keyDownBuf)) {
            listener.onSuccess();
        } else {
            postDelayed(() -> {
                if (null != keyDownBuf)
                    keyDownBuf.delete(0, keyDownBuf.length());
            }, FAST_CLICK_INTERVAL);
        }
    }

    @Override
    public void checkUploadDataDetailsKeyDown(int keyCode, OnKeyDownListener listener) {
        if (keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_F5) {
            keyDownBuf.append("F1");
            checkF1F4KeyEvent(listener);
        }
        if (keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_F8) {
            keyDownBuf.append("F4");
            checkF1F4KeyEvent(listener);
        }
    }

    @Override
    public void aboutKeyDown(int keyCode, OnKeyDownListener listener) {
        if (keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_F5) {
            keyDownBuf.append("F1");
            checkF1F4KeyEvent(listener);
        }
        if (keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_F8) {
            keyDownBuf.append("F4");
            checkF1F4KeyEvent(listener);
        }
    }
}
